package com.we823.cms.repository.service.sys;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.we823.cms.model.sys.Permission;
import com.we823.cms.model.sys.Resource;
import com.we823.cms.model.sys.RoleResourcePermission;
import com.we823.cms.repository.dao.sys.RoleResourcePermissionMapper;
import com.we823.cms.repository.service.AbstractBaseService;
import tk.mybatis.mapper.entity.Example;

@Service
@Scope("prototype")
public class RoleResourcePermissionService extends AbstractBaseService<RoleResourcePermission, Integer> {

	private Logger logger = LoggerFactory.getLogger(RoleResourcePermissionService.class);
	
	private RoleResourcePermissionMapper roleResourcePermissionMapper;

	private @javax.annotation.Resource ResourceService resourceService;
	private @javax.annotation.Resource PermissionService permissionService;
	
	@Autowired
	public RoleResourcePermissionService(RoleResourcePermissionMapper roleResourcePermissionMapper) {
		super(roleResourcePermissionMapper);
		this.roleResourcePermissionMapper = roleResourcePermissionMapper;
	}

	public List<RoleResourcePermission> findByRoleIds(List<Integer> roleIdList){
		Example example = new Example(RoleResourcePermission.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andIn("roleId", roleIdList);
		return roleResourcePermissionMapper.selectByExample(example);
	}

	public void save(
			List<RoleResourcePermission> roleResourcePermissionModels) {
		if (roleResourcePermissionModels==null) return;
		for (RoleResourcePermission roleResourcePermission: roleResourcePermissionModels ) {
			roleResourcePermissionMapper.insert(roleResourcePermission);
		}
	}

	public List<RoleResourcePermission> findByRoleid(int roleId) {
		RoleResourcePermission roleResourcePermission = new RoleResourcePermission();
		roleResourcePermission.setRoleId(roleId);
		return roleResourcePermissionMapper.select(roleResourcePermission);
	}

	public void update(List<RoleResourcePermission> needUpdateList) {
		if (needUpdateList==null) return;
		for(RoleResourcePermission roleResourcePermission : needUpdateList) {
			roleResourcePermissionMapper.insert(roleResourcePermission);
		}
	}

	public void delete(List<RoleResourcePermission> needDeleteList) {
		if (needDeleteList==null) return;
		for(RoleResourcePermission roleResourcePermission : needDeleteList){
			roleResourcePermissionMapper.delete(roleResourcePermission);
		}
	}

	public List<RoleResourcePermission> findByRoleIdIn(
			List<Integer> roleIdList) {
		Example example = new Example(RoleResourcePermission.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andIn("roleId", roleIdList);
		return roleResourcePermissionMapper.selectByExample(example);
	}

	public Set<String> findStringPermissionByRoleIdIn(List<Integer> roleidList) {
		Set<String> permissionSet = Sets.newHashSet();
		List<RoleResourcePermission> roleResourcePermissionModels = findByRoleIdIn(roleidList);
		if (roleResourcePermissionModels!=null && roleResourcePermissionModels.size()>0) {
			String permission = "";
			
			Map<Integer, Resource> resouceMap = resourceService.map();
			Map<Integer, Permission> permissionMap = permissionService.map();
			for (RoleResourcePermission roleResourcePermissionModel : roleResourcePermissionModels) {
				
				String permissionIds = roleResourcePermissionModel.getPermissionIds();
				if(StringUtils.isBlank(permissionIds)) {
					break;
				}
				String[] permissionIdStrings = StringUtils.split(permissionIds,",");
				if(permissionIdStrings!=null && permissionIdStrings.length>0) {
					Resource resourceModel = resouceMap.get(roleResourcePermissionModel.getResourceId());
					for (String permissionId : permissionIdStrings) {
						if(StringUtils.isNumeric(permissionId)) {
							int pId = Integer.parseInt(permissionId);
							if (pId>0) {
								Permission permissionModel = permissionMap.get(pId);
								if(permissionModel!=null && resourceModel!=null) {
									permission = resourceModel.getIdentity()+":"+permissionModel.getIdentity();
									permissionSet.add(permission);
									logger.debug("add permission:{}",permission);
								}
							}
						}
					}
				}
				
			}
		}
		return permissionSet;
	}
	
}
